import axios from "axios"
// import store from '../store/index'
// import router from './router/index.js'

const service =axios.create({
  // 设置默认前缀
  baseURL: '',
  // 请求超时时间
  timeout: 60000,
  headers: {
      'Content-Type': 'application/json'
  }
});

// 工程前缀
// let staticStart = window.configData.project;

// 后端地址
const getBaseUrl = (type) => {
    let baseURL;
    if (process.env.NODE_ENV === 'development') {
        baseURL = '';
    } else if (!type) {
        baseURL = window.configData.BASE_API;
    } 
    service.defaults.baseURL = baseURL;
};



// 请求拦截器
service.interceptors.request.use(
  config => {
    if (window.localStorage.getItem('token')) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
      config.headers.token = localStorage.getItem('token');
    }
    return config;
  }
)

// 响应拦截器
// axios.interceptors.response.use(
//   response => {
//     if (response.status === 200) {
//       return Promise.resolve(response);
//     }
//   },
//   // 服务器状态码不是200的情况 
//   error => {
//     if (error.response.status) {
//       switch (error.response.status) {
//         case 500:
//           router.replace({ name: 'Error_500' })
//           break;
//         case 401:
//           router.replace({ name: 'Error_401' })
//           break;
//         case 404:
//           router.replace({ name: 'Error_404' })
//           break;
//         default:
//           return Promise.reject(error.response);
//       }
//     }
//   }
// );

/** 
* get方法，对应get请求 
*/
export function get(url, params, info = '',type) {
  getBaseUrl(type);
  return new Promise((resolve, reject) => {
    service.get(url + info, {
      params: params
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}

/** 
 * post方法，对应post请求 
 * info为 true，formData格式；
 * info为 undefined或false，是json格式
 */
export function post(url, data = {}, info,type) {
  getBaseUrl(type);
  return new Promise((resolve, reject) => {
    let newData = data
    if (info) {  //  转formData格式
      newData = new FormData();
      for (let i in data) {
        newData.append(i, data[i]);
      }
    }
    service.post(url, newData)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}
export const formpost = (url, params, type) => {
  getBaseUrl(type);
  return new Promise((resolve, reject) => {
      
        let newData = new URLSearchParams();
        for (let i in params) {
          newData.append(i, params[i]);
        }
        
      service.post(staticStart + url, newData, {
          headers: {
              "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
          }
      }).then(res => {
          resolve(res)
      }).catch(err => {
          reject(err);
      })
  })
}
/**
 * 封装put请求
 */

export function put(url, params = {}, info = "") {
  return new Promise((resolve, reject) => {
    axios.put(url + info, params)
      .then(res => {
        resolve(res.data);
      }, err => {
        reject(err.data)
      })
  })
}

/**
* 封装delete请求
*/
export function axiosDelete(url, params = {}, info = "") {
  return new Promise((resolve, reject) => {
    axios.delete(url + info, {
      params: params
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}

// 图片文件上传
export function upFilepost(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios({
        headers: {
          "Content-Type": "application/x-www-urlencoded;charset=UTF-8"
        },
        method: 'post',
        url: url,
        data: data
      }).then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}